//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "event\fn_eventObjectiveTakeOverKilled.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

if (isServer) then {
	private ["_object","_instance_id","_entities","_taken","_hold_count","_count_owners","_count_takers","_count_soldiers","_action","_parameters","_zone_size","_takenover_msg","_lost_msg","_shared_takeover","_hold_time"
			,"_action_id","_owner_side","_taker_side","_takenover_code","_lost_code","_evt_idx","_share_dist","_grp","_leader","_grps","_wp","_taken_time"];
	DEFAULT_PARAM(_params,0,[]);
	DEFAULT_PARAM(_action_id,1,-1);
	
	_object = GETVAL(_params,0,objNull);
	_killer = GETVAL(_params,1,objNull);
	if (!isnull _object) then {
		_action = [_object,_action_id] call FFUNC(actionGet);
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		_instance_id = [_object] call FFUNC(getInstanceID);
		_status = [_object,_action,"TAKEOVER_KILLED_STATUS",-1] call FFUNC(actionGetLocal);
		if (!isNil "_status" && _status < 0) then {
			[_object,_action,"TAKEOVER_KILLED_STATUS",1] call FFUNC(actionSetLocal);
			
			_parameters = [_action] call FFUNC(actionGetParams);
			_zone_size = [_parameters, "ZONE_SIZE", 30,THIS_FILE] call FFUNC(hashGet);
			_takenover_msg = [_parameters, "TAKEN_MSG", "",THIS_FILE] call FFUNC(hashGet);
			_lost_msg = [_parameters, "LOST_MSG", "",THIS_FILE] call FFUNC(hashGet);
			_shared_takeover = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
			_hold_time = [_parameters, "HOLD_TIME", 30,THIS_FILE] call FFUNC(hashGet);
			_owner_side = [_parameters, "OWNER_SIDE", GVAR(side_enemy),THIS_FILE] call FFUNC(hashGet);
			_taker_side = [_parameters, "TAKER_SIDE", GVAR(side_own),THIS_FILE] call FFUNC(hashGet);
			_takenover_code = [_parameters, "TAKEN_CODE", "",THIS_FILE] call FFUNC(hashGet);
			_lost_code = [_parameters, "LOST_CODE", "",THIS_FILE] call FFUNC(hashGet);
			_share_dist = [_parameters, "SHARED_DIST", 300,THIS_FILE] call FFUNC(hashGet);
			_shared_takeover = _shared_takeover call FFUNC(getStringVar);

			if (typename _takenover_msg == typename "") then {_takenover_msg = [_takenover_msg];};
			if (typename _lost_msg == typename "") then {_lost_msg = [_lost_msg];};
			
		};
	};
} else {
	[QGVAR(s_execute),[QUOTE(FFUNC(eventObjectiveTakeOverKilled)),_this],THIS_FILE] call FFUNC(callEvent);
	_evt_idx = [_object,_action,"TAKEOVER_KILLED_STATUS",-1] call FFUNC(actionGetLocal);
	if (!isNil "_evt_idx" && _evt_idx >= 0) then {
		_object removeEventHandler ["killed", _evt_idx];
		[_object,_action,"TAKEOVER_KILLED_STATUS",-1] call FFUNC(actionSetLocal);
	};
};
TRACE("End");
TMFPROFILERSTOP;